//
//  TZEssenceVc.m
//  BSBDJ
//
//  Created by user on 16/5/23.
//  Copyright © 2016年 Burning. All rights reserved.
//

#import "TZEssenceVc.h"
#import "TZShowController.h"
#import "TZToolBarBtn.h"
#define childCount  self.childViewControllers.count

@interface TZEssenceVc ()<UIScrollViewDelegate>


@property (nonatomic, weak) UIScrollView *scrollView;

/** titleView*/
@property (nonatomic ,weak) UIView *titleView;

/** 上一次按钮*/
@property (nonatomic ,weak) UIButton *lastTitleBtn;

/** 索引view*/
@property (nonatomic ,weak) UIView *indexView;

@end


@implementation TZEssenceVc

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //>设置子控制器
    [self setupChildController];
    //>设置ui
    [self setupUI];
}


#pragma mark --------------
#pragma mark 设置UI
- (void)setupUI{
    //>设置顶部view
    [self setupTitleView];
    //>item
    [self setupNavigationItem];
    //>设置scrollView
    [self setupScrollView];

    //>点击第一个按钮
    TZToolBarBtn *firstTitleButotn = self.titleView.subviews.firstObject;
    [self clickTitleBtn:firstTitleButotn];
    
   
}

#warning 如果想要点击状态栏 回到最上面 ,当前控制器只能留下一个scrollsToTop关闭最底部的scrollview的scrollsToTop , 还有就是 为啥 会一次加载多个控制器

#pragma mark 设置scrollView
- (void)setupScrollView
{
    //>不要自动调整scrollView边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = XMGColor(244, 244, 244);
    scrollView.frame = CGRectMake(0, 0,TZScreenW, TZScreenH);
    scrollView.contentSize = CGSizeMake(childCount *TZScreenW, 0);
    scrollView.delegate = self;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.bounces = NO;
    scrollView.scrollsToTop = NO;
    scrollView.pagingEnabled = YES;
    [self.view insertSubview:scrollView atIndex:0  ];
    _scrollView = scrollView;
}
#pragma mark 设置子控制器
- (void)setupChildController
{
    TZShowController *AllVc = [TZShowController new];
    AllVc.title = @"全部";
    AllVc.type = TZTopicTypeAll;
    [self addChildViewController:AllVc];
    
    TZShowController *videoVc = [TZShowController new];
    videoVc.title = @"视频";
    videoVc.type = TZTopicTypeVideo;
    [self addChildViewController:videoVc];
    
    TZShowController *voiceVc = [TZShowController new];
    voiceVc.title = @"声音";
    voiceVc.type = TZTopicTypeVoice;
    [self addChildViewController:voiceVc];
    
    TZShowController *picVc = [TZShowController new];
    picVc.title = @"图片";
    picVc.type = TZTopicTypePicture;
    [self addChildViewController:picVc];
    
    TZShowController *wordVc = [TZShowController new];
    wordVc.title = @"文字";
    wordVc.type = TZTopicTypeWord;
    [self addChildViewController:wordVc];
}
#pragma mark 设置titleView
- (void)setupTitleView
{
    UIView *titleView = [[UIView alloc] init];
    titleView.frame = CGRectMake(0, 64, TZScreenW, 35);
    titleView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleView];
    _titleView = titleView;
    
    //>设置按钮
    [self setupTitleButton];
    [self setupIndexView];
   
}



- (void)setupTitleButton
{
    CGFloat titleBtnW = self.titleView.width/childCount;
    CGFloat titleBtnH = self.titleView.height-2;
    CGFloat titleBtnY = 0;
    for (int i = 0; i <childCount; i++) {
        UIViewController *vc = self.childViewControllers[i];
        TZToolBarBtn *titleButton = [[TZToolBarBtn alloc] init];
        [titleButton setTitle:vc.title forState:0];
        titleButton.frame = CGRectMake(titleBtnW *i, titleBtnY, titleBtnW, titleBtnH);
        titleButton.tag = i;
        titleButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [titleButton addTarget:self action:@selector(clickTitleBtn:) forControlEvents:UIControlEventTouchUpInside];
        [titleButton setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        [titleButton setTitleColor:[UIColor grayColor] forState:0];
        [self.titleView addSubview:titleButton];
    }
}

#pragma mark 索引view
#warning 控件还没显示 一定要计算宽度
- (void)setupIndexView
{
    TZToolBarBtn *titleBtn = self.titleView.subviews.firstObject;
    //>这里一定记得
    [titleBtn.titleLabel sizeToFit];
    UIView *indexView = [[UIView alloc] init];
    indexView.backgroundColor = [UIColor orangeColor];
    indexView.height = 2;
    indexView.width = titleBtn.titleLabel.width;
    indexView.y = self.titleView.height - 2;
    indexView.centerX = titleBtn.centerX;
    
    [self.titleView addSubview:indexView];
    _indexView = indexView;
}

#pragma mark 点击title按钮
- (void)clickTitleBtn:(TZToolBarBtn*)titleBtn
{
    
    // 如果上一次点击的按钮等于当前点击
    if (_lastTitleBtn == titleBtn) {
         [[NSNotificationCenter defaultCenter] postNotificationName:toolBarBtnClickNotificationCenter object:nil];
     
    }
    
    _lastTitleBtn.selected = NO;
    titleBtn.selected = YES;
    _lastTitleBtn = titleBtn;
    
    NSInteger index = titleBtn.tag;
    
   
    
    // 控制scrollView的scrollsToTop属性
    
    
    [UIView animateWithDuration:0.25 animations:^{
        _indexView.centerX = titleBtn.centerX;
        
        // 滑动scrollView到对应的子控制器界面
        CGPoint offset = self.scrollView.contentOffset;
        offset.x = index * self.scrollView.width;
        self.scrollView.contentOffset = offset;
    }completion:^(BOOL finished) {
        
        [self addChildViewIndex:index];
        
    }];
 
    // 控制scrollView的scrollsToTop属性
    for (NSInteger i = 0; i < self.childViewControllers.count; i++) {
        UIViewController *childVc = self.childViewControllers[i];
        
        // 如果控制器的view没有被创建,跳过
        if (!childVc.isViewLoaded) continue;
        
        // 如果控制器的view不是scrollView,就跳过
        if (![childVc.view isKindOfClass:[UIScrollView class]]) continue;
        
        // 如果控制器的view是scrollView
        UIScrollView *scrollView = (UIScrollView *)childVc.view;
        scrollView.scrollsToTop = (i == index);
    }
}

- (void)addChildViewIndex :(NSInteger)index
{
    UITableViewController *vc = self.childViewControllers[index];
    vc.view.frame = self.scrollView.bounds;
    
    [self.scrollView addSubview:vc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / TZScreenW;
    
    TZToolBarBtn *titleBtn = self.titleView.subviews[index];
    
    [self clickTitleBtn:titleBtn];
}
#pragma mark --------------
#pragma mark 清理缓存
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [[SDImageCache sharedImageCache] clearMemory];
}

#pragma mark 设置Item
- (void)setupNavigationItem
{
    
    
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"MainTagSubIcon"] highImage:[UIImage imageNamed:@"MainTagSubIconClick"] target:self action:@selector(clickTagSubIcon)];
    
}
#pragma mark 点击item
- (void)clickTagSubIcon
{
  
}





- (void)dealloc{
    NSLog(@"delloc----EssenceVc");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 1.点击其他按钮跳转到其他页面,将其他控制器的view 加载到当前window上
 */




@end
